package com.woniu113.junk.site.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu113.junk.commons.util.AliyunOSS;
import com.woniu113.junk.site.dto.GoodsInfoDTO;
import com.woniu113.junk.site.exception.GoodsInfoException;
import com.woniu113.junk.site.model.GoodsInfo;
import com.woniu113.junk.site.mapper.GoodsInfoMapper;
import com.woniu113.junk.site.param.goodsinfo.AddGoodsInfoParam;
import com.woniu113.junk.site.param.goodsinfo.AllGoodsInfoParam;
import com.woniu113.junk.site.param.goodsinfo.UpdateGoodsInfoParam;
import com.woniu113.junk.site.service.GoodsInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu113.junk.site.util.Data;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@Service
public class GoodsInfoServiceImpl extends ServiceImpl<GoodsInfoMapper, GoodsInfo> implements GoodsInfoService {
    @Resource
    private AliyunOSS aliyunOSS;

    @Resource
    private GoodsInfoMapper goodsInfoMapper;

    @Override
    public GoodsInfo findByGoodsId(Integer goodsId) {
        //1.判断数据是否为空
        if (goodsId==null){
            throw new GoodsInfoException("商品id不能为空",3001);
        }
        System.out.println(goodsId);
        //2.按照商品id查询商品信息
        GoodsInfo goodsInfo = goodsInfoMapper.selectById(goodsId);
        return goodsInfo;
    }

    @Override
    public void updateNumberByGoodsId(Integer goodsId, Integer reduceNumber) {
        //1.判断数据是否为空
        if (goodsId==null || reduceNumber==null){
            throw new GoodsInfoException("请将数据填写完整",3002);
        }
        //2.根据商品id判断是否存在该商品id的商品数据
        QueryWrapper<GoodsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id",goodsId).eq("state",Data.GOODS_STATE_UP);
        GoodsInfo goodsInfo = goodsInfoMapper.selectOne(queryWrapper);
        if (goodsInfo==null){
            throw new GoodsInfoException("不存在该商品的数据",3003);
        }
        //3.判断要减少的库存数量是否合理
        if (reduceNumber>goodsInfo.getUpNumber()){
            throw new GoodsInfoException("上架库存数量不足",3004);
        }
        //4.更新上架库存量与更新库存量
        goodsInfo.setUpNumber(goodsInfo.getUpNumber()-reduceNumber);
        goodsInfo.setNumber(goodsInfo.getNumber()-reduceNumber);
        goodsInfoMapper.updateById(goodsInfo);
    }

    @Override
    public GoodsInfo addGoodsInfo(AddGoodsInfoParam param) throws Exception {
        //1.检查数据完整性
        if (param.getGoodsImage()==null || param.getGoodsName()==null || param.getGoodsType()==null || param.getGoodsContent()==null || param.getNeedIntegral()==null || param.getNumber()==null){
            throw new GoodsInfoException("请将数据填写完整",3002);
        }
        //2.判断积分是否合理
        if (!(param.getNeedIntegral()>0)){//所需积分不大于0
            throw new GoodsInfoException("商品所需积分不能小于等于0",3005);
        }
        //3.判断库存量是否大于0
        if (param.getNumber()<0){
            throw new GoodsInfoException("商品库存量不能小于0",3006);
        }
        //4.判断是否已存在该商品名(查询商品名相同,商品状态不为删除的)
        QueryWrapper<GoodsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_name",param.getGoodsName()).ne("state", Data.GOODS_STATE_DELETED);
        List<GoodsInfo> goodsInfos = goodsInfoMapper.selectList(queryWrapper);
        if (!goodsInfos.isEmpty()){
            //查询结果不为空,表示有相同商品名存在,此时抛出异常
            throw new GoodsInfoException("要新增的商品名已存在",3007);
        }
        //5.处理图片
        String filePath = aliyunOSS.ProcessPicture(param.getGoodsImage());
        //6.录入数据,执行新增操作
        GoodsInfo goodsInfo = new GoodsInfo();
        goodsInfo.setGoodsName(param.getGoodsName());
        goodsInfo.setGoodsType(param.getGoodsType());
        goodsInfo.setGoodsContent(param.getGoodsContent());
        goodsInfo.setNeedIntegral(param.getNeedIntegral());
        goodsInfo.setNumber(param.getNumber());
        goodsInfo.setGoodsImage(filePath);
        goodsInfo.setState(Data.GOODS_STATE_DOWN);
        goodsInfoMapper.insert(goodsInfo);
        return goodsInfo;
    }

    @Override
    public void upGoodsInfo(Integer goodsId, Integer upNumber) {
        //1.判断数据完整性
        if (goodsId ==null || upNumber==null){
            throw new GoodsInfoException("请将数据填写完整",3002);
        }
        //2.判断要上架的商品数量是否大于0(要上架的商品数量不能小于0也不能等于0)
        if (upNumber<=0){
            throw new GoodsInfoException("要上架的商品数量不合理",3008);
        }
        //3.判断该goodsId对应的商品数据是否存在(状态只能为待上架(下架),上架的不能再上架,删除的也不能上架)
        QueryWrapper<GoodsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id",goodsId).eq("state",Data.GOODS_STATE_DOWN);
        GoodsInfo goodsInfo = goodsInfoMapper.selectOne(queryWrapper);
        if (goodsInfo==null){//查出来的数据为空,表示不存在该goodsId对应的符合上架条件的商品数据
            throw new GoodsInfoException("这个商品不符合上架条件",3009);
        }
        //4.判断要上架的商品数量是否合理(不能小于商品库存)
        if (upNumber>goodsInfo.getNumber()){
            throw new GoodsInfoException("库存数量不足",3010);
        }
        //5.修改商品状态为上架,填入上架数量
        goodsInfo.setState(Data.GOODS_STATE_UP);
        goodsInfo.setUpNumber(upNumber);
        goodsInfoMapper.updateById(goodsInfo);
    }

    @Override
    public void downGoodsInfo(Integer goodsId) {
        //1.判断数据完整性
        if (goodsId ==null){
            throw new GoodsInfoException("请将数据填写完整",3002);
        }
        //2.判断该goodsId对应的商品数据是否存在(状态只能为上架)
        QueryWrapper<GoodsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id",goodsId).eq("state",Data.GOODS_STATE_UP);
        GoodsInfo goodsInfo = goodsInfoMapper.selectOne(queryWrapper);
        if (goodsInfo==null){//查出来的数据为空,表示不存在该goodsId对应的符合下架条件的商品数据
            throw new GoodsInfoException("这个商品不符合下架条件",3011);
        }
        //3.修改商品状态为下架(待上架),修改上架数量为0
        goodsInfo.setState(Data.GOODS_STATE_DOWN);
        goodsInfo.setUpNumber(0);
        goodsInfoMapper.updateById(goodsInfo);
    }

    @Override
    public void deleteGoodsInfo(Integer goodsId) {
        //1.判断数据完整性
        if (goodsId ==null){
            throw new GoodsInfoException("请将数据填写完整",3002);
        }
        //2.判断该goodsId对应的商品数据是否存在(状态不能为删除)
        QueryWrapper<GoodsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id",goodsId).ne("state",Data.GOODS_STATE_DELETED);
        GoodsInfo goodsInfo = goodsInfoMapper.selectOne(queryWrapper);
        if (goodsInfo==null){//查出来的数据为空,表示不存在该goodsId对应的符合删除条件的商品数据
            throw new GoodsInfoException("这个商品不符合删除条件",3012);
        }
        //3.修改商品状态为删除
        goodsInfo.setState(Data.GOODS_STATE_DELETED);
        goodsInfoMapper.updateById(goodsInfo);
    }

    @Override
    public void updateGoodsInfo(UpdateGoodsInfoParam param) {
        //1.判断修改的积分是否合理
        if (!(param.getNeedIntegral()>0)){//所需积分不大于0
            throw new GoodsInfoException("商品所需积分不能小于等于0",3005);
        }
        //2.判断修改的库存量是否合理
        if (param.getNumber()<0){
            throw new GoodsInfoException("商品库存量不能小于0",3006);
        }
        //3.判断要修改的商品是否存在(需要状态不为删除的)
        QueryWrapper<GoodsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id",param.getGoodsId()).ne("state",Data.GOODS_STATE_DELETED);
        GoodsInfo goodsInfo = goodsInfoMapper.selectOne(queryWrapper);
        if (goodsInfo==null){//为空表示不存在课修改的商品
            throw new GoodsInfoException("该商品不符合更新条件",3013);
        }
        //4.修改商品信息
        goodsInfo.setGoodsContent(param.getGoodsContent());
        goodsInfo.setNumber(param.getNumber());
        goodsInfo.setNeedIntegral(param.getNeedIntegral());
        goodsInfoMapper.updateById(goodsInfo);
    }

    @Override
    public GoodsInfoDTO findAll(AllGoodsInfoParam param) {
        System.out.println("Goods Name: " + param.getGoodsName());
        System.out.println("Goods Type: " + param.getGoodsType());
        int offset = (param.getPageNum() - 1) * param.getPageSize();
        QueryWrapper<GoodsInfo> wrapper = new QueryWrapper<>();

        if (param.getGoodsName() != null && !param.getGoodsName().trim().isEmpty()) {
            wrapper.like("goods_name", param.getGoodsName());
        }
        if (param.getGoodsType() != null && !param.getGoodsType().trim().isEmpty()) {
            wrapper.eq("goods_type", param.getGoodsType());
        }
        wrapper.eq("state", Data.GOODS_STATE_UP);

        // 获取总记录数
        long total = goodsInfoMapper.selectCount(wrapper);
        // 添加分页
        wrapper.last("LIMIT " + offset + ", " + param.getPageSize());
        List<GoodsInfo> infos = goodsInfoMapper.selectList(wrapper);
        String sql = wrapper.getSqlSegment(); // 获取 SQL 片段
        System.out.println("Generated SQL: " + sql);
        GoodsInfoDTO dto = new GoodsInfoDTO();
        dto.setList(infos);
        dto.setTotal(Integer.valueOf((int) total));  // 设置总记录数
        System.out.println(dto.getList());
        System.out.println(dto.getTotal());
        return dto;
    }

    @Override
    public GoodsInfoDTO findAllByAdmin(AllGoodsInfoParam param) {
        System.out.println("Goods Name: " + param.getGoodsName());
        System.out.println("Goods Type: " + param.getGoodsType());
        int offset = (param.getPageNum() - 1) * param.getPageSize();
        QueryWrapper<GoodsInfo> wrapper = new QueryWrapper<>();

        if (param.getGoodsName() != null && !param.getGoodsName().trim().isEmpty()) {
            wrapper.like("goods_name", param.getGoodsName());
        }
        if (param.getGoodsType() != null && !param.getGoodsType().trim().isEmpty()) {
            wrapper.eq("goods_type", param.getGoodsType());
        }
        wrapper.ne("state", Data.GOODS_STATE_DELETED);

        // 获取总记录数
        long total = goodsInfoMapper.selectCount(wrapper);
        // 添加分页
        wrapper.last("LIMIT " + offset + ", " + param.getPageSize());
        List<GoodsInfo> infos = goodsInfoMapper.selectList(wrapper);
        String sql = wrapper.getSqlSegment(); // 获取 SQL 片段
        System.out.println("Generated SQL: " + sql);
        GoodsInfoDTO dto = new GoodsInfoDTO();
        dto.setList(infos);
        dto.setTotal(Integer.valueOf((int) total));  // 设置总记录数
        System.out.println(dto.getList());
        System.out.println(dto.getTotal());
        return dto;
    }

}
